Skip to content

detect color support in terminal#21

Merged
jbsmith7741 merged 1 commit intomainfrom
terminal
Mar 10, 2026
Merged

detect color support in terminal#21
jbsmith7741 merged 1 commit intomainfrom
terminal

Conversation

@jbsmith7741
Copy link
Member

@jbsmith7741 jbsmith7741 commented Jan 30, 2026

PR Type

Enhancement


Description

  • Add color support detection for terminal output

  • Implement useColor() function respecting NO_COLOR and FORCE_COLOR standards

  • Create colorRed() and colorGreen() helper functions for ANSI coloring

  • Replace hardcoded ANSI codes with color helper functions throughout codebase

  • Add test for color diagnostics functionality


Diagram Walkthrough

flowchart LR
  A["Environment Variables<br/>NO_COLOR, FORCE_COLOR, TERM"] -->|checked by| B["useColor()"]
  B -->|returns bool| C["colorRed() & colorGreen()"]
  C -->|applied to| D["Test Output Messages"]
  D -->|displayed in| E["Terminal with/without colors"]
Loading

File Walkthrough

Relevant files
Enhancement
trial.go
Add color detection and helper functions                                 

trial.go

  • Added os import for environment variable access
  • Implemented useColor() function to detect color support based on
    NO_COLOR, FORCE_COLOR, and TERM environment variables
  • Created colorRed() and colorGreen() helper functions wrapping ANSI
    color codes
  • Replaced hardcoded ANSI escape sequences with color helper function
    calls in SubTest() and Test() methods
+37/-3   
Tests
trial_test.go
Update tests for color support detection                                 

trial_test.go

  • Added fmt and os imports for testing color functionality
  • Updated TestTrial_TestCase() to use colorRed() and colorGreen()
    helpers instead of hardcoded ANSI codes
  • Simplified TestParallel() by removing nil check and chaining method
    calls
  • Replaced TestParallel_Chaining() with new TestColorDiagnostics() test
    that verifies color detection logic
+16/-23 

@qodo-code-review
Copy link

qodo-code-review bot commented Jan 30, 2026

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
🟢
No security concerns identified No security vulnerabilities detected by AI analysis. Human verification advised for critical code.
Ticket Compliance
🎫 No ticket provided
  • Create ticket/issue
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

  • Update
Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@qodo-code-review
Copy link

qodo-code-review bot commented Jan 30, 2026

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
Cache color detection result

Cache the result of useColor() in a package-level variable like colorEnabled and
use it within colorRed to prevent repeated environment variable lookups.

trial.go [34-39]

+var colorEnabled = useColor()
+
 func colorRed(s string) string {
-	if useColor() {
+	if colorEnabled {
 		return "\033[31m" + s + "\033[39m"
 	}
 	return s
 }
  • Apply / Chat
Suggestion importance[1-10]: 6

__

Why: The suggestion correctly identifies that repeatedly calling useColor() is inefficient and proposes caching the result, which is a good performance optimization for the new functionality.

Low
Use full ANSI reset

Replace the ANSI code \033[39m with the full reset code \033[0m to ensure all
text attributes are cleared, preventing unintended color bleed.

trial.go [36]

-return "\033[31m" + s + "\033[39m"
+return "\033[31m" + s + "\033[0m"
  • Apply / Chat
Suggestion importance[1-10]: 5

__

Why: The suggestion correctly points out that using the full reset code \033[0m is more robust for clearing all text attributes, preventing potential color or style bleeding from other sources.

Low
  • Update

@jbsmith7741 jbsmith7741 merged commit 1d296ea into main Mar 10, 2026
16 checks passed
@jbsmith7741 jbsmith7741 deleted the terminal branch March 10, 2026 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant